package com.sara.service.impl;

import com.sara.entity.*;
import com.sara.exception.ServiceException;
import com.sara.mapper.EmployeeRoleMapper;
import com.sara.mapper.PermissionMapper;
import com.sara.mapper.RoleMapper;
import com.sara.mapper.RolePermissionMapper;
import com.sara.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Wayne
 * @date 2019/1/5
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    /**
     * 查找所有角色
     *
     * @return role 的集合
     */
    @Override
    public List<Role> findAllRole() {


        return roleMapper.selectWithPermission();
    }

    /**
     * 新增角色，但是会在角色权限的关联关系表里面也存入关联关系
     *
     * @param role
     * @param permissionId
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveRole(Role role, Integer[] permissionId) {
        roleMapper.insertSelective(role);
        for(Integer permissionIds : permissionId){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(permissionIds);
            rolePermission.setRoleId(role.getId());
            rolePermissionMapper.insertSelective(rolePermission);
        }
    }

    @Override
    public Role findRoleByRoleId(Integer id) {
        List<Permission> newList = new ArrayList<>();
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        rolePermissionExample.createCriteria().andRoleIdEqualTo(id);
        List<RolePermission> rolePermissionList = rolePermissionMapper.selectByExample(rolePermissionExample);
        for(RolePermission rolePermission : rolePermissionList){
            PermissionExample permissionExample = new PermissionExample();
            permissionExample.createCriteria().andIdEqualTo(rolePermission.getPermissionId());
            List<Permission> permissionList = permissionMapper.selectByExample(permissionExample);
            for(Permission permission : permissionList){
                newList.add(permission);
            }
        }

        Role role = roleMapper.selectByPrimaryKey(id);
        role.setPermissionList(newList);
        return role;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void edit(Role role, Integer[] permissionId) {

        roleMapper.updateByPrimaryKey(role);
        if(permissionId.length > 0){
            RolePermissionExample rolePermissionExample = new RolePermissionExample();
            rolePermissionExample.createCriteria().andRoleIdEqualTo(role.getId());
            rolePermissionMapper.deleteByExample(rolePermissionExample);
            roleMapper.updateByPrimaryKey(role);
            for(Integer permissionIds : permissionId){
                RolePermission rolePermission = new RolePermission();
                rolePermission.setPermissionId(permissionIds);
                rolePermission.setRoleId(role.getId());
                rolePermissionMapper.insertSelective(rolePermission);
            }

        }



    }

    @Override
    public void deleteByRoleId(Integer id) throws ServiceException {
        Role roleByRoleId = roleService.findRoleByRoleId(id);
        if(roleByRoleId == null){
            throw new  ServiceException("参数异常！");
        }

        EmployeeRoleExample employeeRoleExample = new EmployeeRoleExample();
        employeeRoleExample.createCriteria().andRoleIdEqualTo(id);
        List<EmployeeRole> employeeRoles = employeeRoleMapper.selectByExample(employeeRoleExample);
        if(employeeRoles!= null && employeeRoles.size() > 0){
            throw new ServiceException("该角色已被员工绑定,请解除绑定后重新操作!");
        }

        roleMapper.deleteByPrimaryKey(id);
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        rolePermissionExample.createCriteria().andRoleIdEqualTo(id);
        rolePermissionMapper.deleteByExample(rolePermissionExample);
    }


}
